%% Author: vjache
%% Created: Nov 9, 2009
%% Description: TODO: Add description to pipe_tracer
-module(pipe_tracer).

%%
%% Include files
%%
-include("../include/pipe.hrl").
%%
%% Exported Functions
%%
-export([
		 new/2,
		 open/1,
		 push/2,
		 flush/1,
		 close/1,
		 test/1]).

%%====================================================================
%% API Functions
%%====================================================================

new(TargetPipe,LogPipe) ->
	pipe:pipe_from_module(?MODULE, TargetPipe, LogPipe).

open({ TargetPipe, LogPipe}) -> %%PipeState
	S=pipe:open(TargetPipe),
    {S,pipe:push(pipe:open(LogPipe),{open,S})}.
push({TargetPipeState,LogPipeState},Item) ->
	TargetPipeState1=pipe:push(TargetPipeState,Item),
	LogPipeState1=pipe:push(LogPipeState,{Item,TargetPipeState1}),
	{TargetPipeState1,LogPipeState1}.
flush({TargetPipeState,LogPipeState}) ->
	TargetPipeState1=pipe:flush(TargetPipeState),
	LogPipeState1=pipe:push(LogPipeState,{flush,TargetPipeState1}),
	{TargetPipeState1,LogPipeState1}.
close({TargetPipeState,LogPipeState}) ->
	TargetPipeState1=pipe:close(TargetPipeState),
	pipe:close(pipe:push(LogPipeState,{close,TargetPipeState1})).

%%====================================================================
%% Test Functions
%%====================================================================

test(L) ->
	S=pipe:open(new(fun(Item)->
							Item < 10 
	end ,pipe_console:new())),
	{IterState,PipeState}=pipe:push_each(S, iter:from_list(L)),
	iter:close(IterState),
	pipe:close(PipeState).



%%
%% Local Functions
%%

